﻿using MediatR;
using SqlSugar;
using System.IO.Pipelines;
using WarehouseManagement.Application.Command.WarehouseSAppCommand;
using WarehouseManagement.Domain.DTO.WarehouseSApp;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.WarehouseSAppCommandHandler
{
    public class ListWarehouseSAppCommandHandler:IRequestHandler<ListWarehouseSAppCommand,List<ListWarehouseSAppDto>>
    {
        private readonly lnterfaceWarehouseSAppRepository _warehouseSAppRepository;//仓库入库申请表
        private readonly lnterfaceStorageProductRepository _storageProductsRepository;//仓库入库申请产品明细表

        public ListWarehouseSAppCommandHandler(lnterfaceWarehouseSAppRepository warehouseSAppRepository, lnterfaceStorageProductRepository storageProductsRepository)
        {
            _warehouseSAppRepository = warehouseSAppRepository;
            _storageProductsRepository = storageProductsRepository;
        }

        public async Task<List<ListWarehouseSAppDto>> Handle(ListWarehouseSAppCommand request, CancellationToken cancellationToken)
        {
            var res = (await _warehouseSAppRepository.GetAll()).Where(x => x.IsDel == false).ToList();
            var product = (await _storageProductsRepository.GetAll()).ToList();
            var list = from a in res
                       select new ListWarehouseSAppDto
                       {
                           ShippRequestAppId=a.ShippRequestAppId,
                           StorageTopic=a.StorageTopic,
                           StorageNumber=a.StorageNumber,
                           StorageSource=a.StorageSource,
                           WName=a.WName,
                           AppName=a.AppName,
                           AppDate=a.AppDate,
                           StorageType=a.StorageType,
                           VName=a.VName,
                           StorageRemark=a.StorageRemark,
                           State=a.State,
                           StripNumber=a.StripNumber,
                           list=(from b in product
                                where a.StorageNumber==b.StorageNumber
                                select new StorageProduct
                                {
                                    StorageProductId=b.StorageProductId,
                                    StorageNumber=b.StorageNumber,
                                    PNumber=b.PNumber,
                                    PName=b.PName,
                                    PSpec=b.PSpec,
                                    PUnit=b.PUnit,
                                    PPrice=b.PPrice,
                                    SPConfirmNum=b.SPConfirmNum,
                                    SumPrice=b.SumPrice,
                                    Batch=b.Batch,
                                    CreateDate=b.CreateDate,
                                    ValidDate=b.ValidDate,
                                    ArriveDate=b.ArriveDate,
                                    WrapName=b.WrapName,
                                    Remark=b.Remark,
                                    SumNum=b.SumNum
                                }).ToList()
                       };
            return list.ToList();
        }
    }
}
